<template>
  <CommonPage>
    <NCard>
      <NTabs type="line" animated>
        <NTabPane name="normal" tab="一般工业相机">
          <NForm
            ref="normalFormRef"
            :model="normalForm"
            :rules="rules"
            label-placement="left"
            :label-width="140"
          >
            <NFormItem label="图像采集帧率" path="frame_rate">
              <NInputNumber
                v-model:value="normalForm.frame_rate"
                :min="1"
                :precision="0"
                :show-button="false"
                placeholder="请输入图像采集帧率"
              />
            </NFormItem>
            <NFormItem label="图像采集曝光时间" path="exposure_time">
              <div style="display: flex; align-items: center;">
                <NInputNumber
                  v-model:value="normalForm.exposure_time"
                  :min="1"
                  :precision="0"
                  :show-button="false"
                  placeholder="请输入图像采集曝光时间"
                />
                <span style="margin-left: 4px">µs</span>
              </div>
            </NFormItem>
            <NFormItem label="增益" path="gain">
              <div style="display: flex; align-items: center;">
                <NInputNumber
                  v-model:value="normalForm.gain"
                  :min="0"
                  :precision="0"
                  :show-button="false"
                  placeholder="请输入增益"
                />
                <span style="margin-left: 4px">dB</span>
              </div>
            </NFormItem>
            <NFormItem label="自动增益" path="agc">
              <NSwitch v-model:value="normalForm.agc" />
            </NFormItem>
            <NFormItem label="分辨率" path="resolution">
              <div style="display: flex; align-items: center;">
                <NInputNumber
                  v-model:value="normalForm.width"
                  :min="1"
                  :precision="0"
                  :show-button="false"
                  placeholder="宽度"
                  style="width: 80px"
                />
                <span style="margin: 0 4px">x</span>
                <NInputNumber
                  v-model:value="normalForm.height"
                  :min="1"
                  :precision="0"
                  :show-button="false"
                  placeholder="高度"
                  style="width: 80px"
                />
              </div>
            </NFormItem>
          </NForm>
        </NTabPane>

        <NTabPane name="high_speed" tab="高速工业相机">
          <NForm
            ref="highSpeedFormRef"
            :model="highSpeedForm"
            :rules="rules"
            label-placement="left"
            :label-width="140"
          >
            <NFormItem label="图像采集帧率" path="frame_rate">
              <NInputNumber
                v-model:value="highSpeedForm.frame_rate"
                :min="1"
                :precision="0"
                :show-button="false"
                placeholder="请输入图像采集帧率"
              />
            </NFormItem>
            <NFormItem label="图像采集曝光时间" path="exposure_time">
              <div style="display: flex; align-items: center;">
                <NInputNumber
                  v-model:value="highSpeedForm.exposure_time"
                  :min="1"
                  :precision="0"
                  :show-button="false"
                  placeholder="请输入图像采集曝光时间"
                />
                <span style="margin-left: 4px">µs</span>
              </div>
            </NFormItem>
            <NFormItem label="增益" path="gain">
              <div style="display: flex; align-items: center;">
                <NInputNumber
                  v-model:value="highSpeedForm.gain"
                  :min="0"
                  :precision="0"
                  :show-button="false"
                  placeholder="请输入增益"
                />
                <span style="margin-left: 4px">dB</span>
              </div>
            </NFormItem>
            <NFormItem label="自动增益" path="agc">
              <NSwitch v-model:value="highSpeedForm.agc" />
            </NFormItem>
            <NFormItem label="分辨率" path="resolution">
              <div style="display: flex; align-items: center;">
                <NInputNumber
                  v-model:value="highSpeedForm.width"
                  :min="1"
                  :precision="0"
                  :show-button="false"
                  placeholder="宽度"
                  style="width: 80px"
                />
                <span style="margin: 0 4px">x</span>
                <NInputNumber
                  v-model:value="highSpeedForm.height"
                  :min="1"
                  :precision="0"
                  :show-button="false"
                  placeholder="高度"
                  style="width: 80px"
                />
              </div>
            </NFormItem>
          </NForm>
        </NTabPane>
      </NTabs>

      <div mt-20>
        <NButton type="primary" :loading="loading" @click="handleSave">保存</NButton>
      </div>
    </NCard>
  </CommonPage>
</template>

<script setup>
import { h, onMounted, ref } from 'vue'
import { NButton, NInput, NInputNumber, NFormItem, NSwitch, NSelect, NForm, NCard, NTabs, NTabPane } from 'naive-ui'
import CommonPage from '@/components/page/CommonPage.vue'
import api from '@/api/camera'

defineOptions({ name: '相机参数设置' })

const loading = ref(false)
const normalFormRef = ref(null)
const highSpeedFormRef = ref(null)

const normalForm = ref({
  camera_type: 'MV-CS050-10GM-PRO',
  frame_rate: null,
  exposure_time: null,
  gain: null,
  agc: false,
  width: null,
  height: null,
})

const highSpeedForm = ref({
  camera_type: 'MV-CS004-10UC',
  frame_rate: null,
  exposure_time: null,
  gain: null,
  agc: false,
  width: null,
  height: null,
})

const rules = {
  frame_rate: {
    required: true,
    type: 'number',
    message: '请输入图像采集帧率',
    trigger: ['blur', 'input'],
  },
  exposure_time: {
    required: true,
    type: 'number', 
    message: '请输入图像采集曝光时间',
    trigger: ['blur', 'input'],
  },
  gain: {
    required: true,
    type: 'number',
    message: '请输入增益',
    trigger: ['blur', 'input'],
  },
  width: {
    required: true,
    type: 'number',
    message: '请输入分辨率宽度',
    trigger: ['blur', 'input'],
  },
  height: {
    required: true,
    type: 'number',
    message: '请输入分辨率高度',
    trigger: ['blur', 'input'],
  },
}

onMounted(async () => {
  try {
    const [normalConfig, highSpeedConfig] = await Promise.all([
      api.getCameraConfig('MV-CS050-10GM-PRO'),
      api.getCameraConfig('MV-CS004-10UC'),
    ])
    if (normalConfig?.data) {
      const [width, height] = (normalConfig.data.resolution || '').split('x')
      normalForm.value = {
        ...normalConfig.data,
        width: parseInt(width) || null,
        height: parseInt(height) || null,
      }
    }
    if (highSpeedConfig?.data) {
      const [width, height] = (highSpeedConfig.data.resolution || '').split('x')
      highSpeedForm.value = {
        ...highSpeedConfig.data,
        width: parseInt(width) || null,
        height: parseInt(height) || null,
      }
    }
  } catch (error) {
    window.$message.error('获取相机配置失败')
  }
})

async function handleSave() {
  try {
    loading.value = true
    await Promise.all([
      normalFormRef.value?.validate(),
      highSpeedFormRef.value?.validate(),
    ])
    
    const normalData = {
      ...normalForm.value,
      resolution: `${normalForm.value.width}x${normalForm.value.height}`,
    }
    delete normalData.width
    delete normalData.height

    const highSpeedData = {
      ...highSpeedForm.value,
      resolution: `${highSpeedForm.value.width}x${highSpeedForm.value.height}`,
    }
    delete highSpeedData.width
    delete highSpeedData.height
    
    await Promise.all([
      api.updateCameraConfig(normalData),
      api.updateCameraConfig(highSpeedData),
    ])
    
    window.$message.success('保存成功')
  } catch (error) {
    console.error(error)
  } finally {
    loading.value = false
  }
}
</script> 